package com.lp.sys.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lp.business.model.Access;
import com.lp.business.model.Menu;
import com.lp.business.model.User;
import com.lp.business.service.MenuMethodService;
import com.lp.business.service.MenuService;
import com.lp.business.service.UserService;

/**
 * Hplus系统后台框架控制器
 * 
 * @author lp
 * 
 */
@Controller
@RequestMapping("/admin/frame")
public class FrameController {
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "menuService")
	private MenuService menuService;
	@Resource(name = "menuMethodService")
	private MenuMethodService menuMethodService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}

	/**
	 * 进入后台首页
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "index", method = { RequestMethod.POST, RequestMethod.GET })
	public String index(HttpServletRequest request, Model model) {
		User u = (User) request.getSession().getAttribute("adminSessionInfo");
		model.addAttribute("userDateInfo", u);
		@SuppressWarnings("unchecked")
		List<Access> accesses = (List<Access>) request.getSession().getAttribute("accessSessionInfo");
		List<Menu> menus = this.menuService.getUserMenuList(accesses);
		model.addAttribute("menuList", menus);
		return "/frame/index";
	}
	
	/**
	 * Hplus框架主体内容部分
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "body", method = { RequestMethod.POST, RequestMethod.GET })
	public String body(HttpServletRequest request, Model model) {
		return "/frame/body";
	}

	/**
	 * Hplus框架左侧菜单栏部分
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "left", method = { RequestMethod.POST, RequestMethod.GET })
	public String left(HttpServletRequest request, Model model) {
		return "/frame/left";
	}
	
	/**
	 * Hplus框架右侧侧边栏部分
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "rightSidebar", method = { RequestMethod.POST, RequestMethod.GET })
	public String rightSidebar(HttpServletRequest request, Model model) {
		return "/frame/rightSidebar";
	}
	
	/**
	 * Hplus框架顶部页头部分
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "top", method = { RequestMethod.POST, RequestMethod.GET })
	public String top(HttpServletRequest request, Model model) {
		return "/frame/top";
	}
	
	
	
	/**
	 * Hplus框架底部页脚部分
	 * 
	 * @param request
	 *            请求对象
	 * @param model
	 *            spring UI 模型
	 * @return String 模板字符串
	 */
	@RequestMapping(value = "foot", method = { RequestMethod.POST, RequestMethod.GET })
	public String foot(HttpServletRequest request, Model model) {
		return "/frame/foot";
	}
	
}
